<template>
    <!-- 商品展示模板 -->
    <div class="goods" v-if="!isNotFound">
        <!-- 商品轮播 -->
        <van-swipe class="goods-swipe" :autoplay="3000">
            <van-swipe-item v-for="item in album" :key="item.id">
                <img :src="item.picture" height="414">
            </van-swipe-item>
        </van-swipe>
        <van-cell-group>
            <van-cell>
                <template #title>
                    <span class="goods-top">新品</span>
                    <div class="goods-price">
                        <span class="small">￥</span>
                        {{ goods.price }}
                        <span class="spec">{{ goods.spec }}</span>
                    </div>
                    <div class="goods-title">
                        <span class="small"> {{ goods.name }}</span>
                    </div>
                </template>
            </van-cell>
            <van-cell class="goods-express">
                <template #title>
                    <van-col span="10">运费：10</van-col>
                    <van-col span="14">剩余： {{ goods.stock }}</van-col>
                </template>
            </van-cell>
        </van-cell-group>
        <van-cell-group class="goods-cell-group">
            <van-cell>
                <template #title>
                    <span class="van-cell-text">发货  陕西宝鸡</span>
                </template>
            </van-cell>
            <van-cell>
                <template #title>
                    <span class="van-cell-text">保障  坏单包赔 ·假一赔四 ·极速退款</span>
                </template>
            </van-cell>
            <van-cell>
                <template #title>
                    <span class="van-cell-text">参数  品牌：枝纯  价格：100-200</span>
                </template>
            </van-cell>
        </van-cell-group>
        <div class="goods-cell-title">
            —— 宝贝详情 —— <!-- 商品详情标题 -->
        </div>
        <div class="goods-description" v-html="goods.description"></div>
        <!-- 底部按钮-->
        <van-action-bar>
            <van-action-bar-icon icon="chat-o" text="客服" @click="good"/>
            <van-action-bar-icon icon="cart-o" :badge="cartCount()" text="购物车" @click="onClickCart"/>
            <van-action-bar-button type="warning" text="加入购物车"  @click="addCart"/>
            <van-action-bar-button type="danger" text="立即购买" @click="sorry"/>
        </van-action-bar>
    </div>
    <div class="goods-not-found" v-else>商品不存在</div>
</template>

<script setup>
import { reactive, ref,onMounted } from 'vue'
import { getGoodsAlbum, getGoodsDetail } from '../api'
import { showToast } from 'vant'
import { useRouter } from 'vue-router'
import useCart from '../stores/cart'

const { cartCount, addToCart } = useCart()
const goods = reactive({}) // 响应式商品对象
const album = ref([]) // 商品相册
const isNotFound = ref(false) // 商品是否存在标志
const router = useRouter(); // 路由实例

const props = defineProps({
    id: String // 商品ID
})
    
onMounted(() => {
    loadGoodsDetail () // 组件挂载后加载商品详情
})
    
// 加载商品详情
const loadGoodsDetail = async () => {
    const data1 = await getGoodsDetail({ id: props.id }) // 获取商品详情
    if (!data1.id) {
        isNotFound.value = true // 如果没有商品，则设置为未找到
        return
    }
    const data2 = await getGoodsAlbum({ goods_id: props.id }) // 获取商品相册
    if (data2.length === 0 && data1.picture !== '') {
        data2.push({ picture: data1.picture }) // 如果相册为空且有主图，则添加主图
    }
    Object.assign(goods, data1) // 将商品详情合并到响应式对象
    album.value = data2 // 更新相册
}

const sorry = () => {
    router.push({ name: 'purchase' }) // 跳转到购买页面
}
const onClickCart = () => {
    router.push({ name: 'cart' }) // 跳转到购物车页面
}
const addCart = () => {
    addToCart({ id: props.id, num: 1, checked: true }) // 将商品添加到购物车
    showToast({ message: '添加成功' }) // 显示添加成功的提示
}
const good = () => {
    showToast({ message: '别找客服' }) // 显示客服提示
}
</script>

<style lang="less" scoped>
.goods {
    text-align: center;
    padding-bottom: 50px;
    .goods-swipe {
        img {
            width: 100%;
            display: block; // 图片块级展示
        }
    }
    .goods-top {
        display: block;
        width: 30px;
        padding: 0 5px;
        border-radius: 10px;
        color: #fff;
        background-color: #f44; // 新品标签样式
    }
    .goods-title {
        text-align: left;
        .small {
            font-size: 14px;
        }
    }
    .goods-price {
        color: #f44;
        text-align: left;
        font-size: 20px; 
        .small {
            font-size: 12px;
        }
        .spec {
            font-size: 12px;
            color: #999;
        }
    }
    .goods-cell-title {
        padding: 20px 0; // 商品详情标题样式
    }
    .goods-description {
        width: 95%;
        margin: 0 auto;
        padding-bottom: 20px;
        font-size: 14px;
        :deep(img) {
            max-width: 100%;
            height: auto; // 图片自适应
            display: block;
        }
    }
    &-express {
        color: #999;
        font-size: 12px;
        padding: 5px 15px;
        :deep(.van-col) {
            float: left; // 列浮动
        }
        :deep(.van-col--14){
            width: 58%;
        }
    }
    .goods-cell-group {
        :deep(.van-cell_title span){
            float: left; // 单元格标题浮动
        }
    }
    :deep(.van-cell:after) {
        border: none; // 去除单元格下边框
    }
}
.goods-not-found {
    padding-top: 48px; 
    text-align: center; // 商品未找到样式
    font-size: 28px;
}
</style>